package com.zhongwang.business.module.quoteManage.quoteManageService;


import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.gen.entity.VerifyProcess;
import com.zhongwang.business.gen.mapper.FileManageMapper;
import com.zhongwang.business.gen.mapper.TaskMapper;
import com.zhongwang.business.gen.mapper.VerifyProcessMapper;
import com.zhongwang.business.gen.mapper.QuotationMainMapper;
import com.zhongwang.business.module.basic.model.VerifyProcessModel;
import com.zhongwang.business.module.quoteManage.quoteManageDao.QuotationMainDao;
import com.zhongwang.business.module.quoteManage.quoteManageDao.QuotationApproveDao;
import com.zhongwang.business.module.quoteManage.quoteManageDao.QuotationQueryDao;
import com.zhongwang.business.module.quoteManage.quoteManageModel.QuotationMainModel;
import com.zhongwang.business.module.quoteManage.quoteManageModel.QuotationSublistModel;
import com.zhongwang.business.gen.entity.QuotationMain;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.zhongwang.business.module.system.log.LogService;
import java.util.Date;
import java.util.List;

/**
 *  报价审核管理 服务层
 * @author zcy
 * @date 2020/10/24
 */
@Log4j2
@Service
public class QuoteApproveService {
    @Autowired
    private QuotationMainDao quotationMainDao;
    @Autowired
    private QuotationApproveDao quotationApproveDao;
    @Autowired
    private QuotationQueryDao quotationQueryDao;
    @Autowired
    private FileManageMapper fileManageMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private QuotationMainMapper quotationMainMapper;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    public LogService logService;

    @Value("${user.upload.path}")
    private String uploadPath;
    /**
     * 审核列表
     * @param query
     * @return
     */
    public List<QuotationMainModel> findQttApproveList(QuotationMainModel query) {
        List<QuotationMainModel> quotationMainModelList = quotationApproveDao.selectQuoteApproveList(query);
        int m = quotationMainModelList.size();
        for(int i = 0; i < m; i++){
            List<QuotationSublistModel> quotationSublistModelList = quotationQueryDao.selectQttProNameList(quotationMainModelList.get(i).getQuoteId());
            String name = "";
            String code = "";
            for (int k= 0; k < quotationSublistModelList.size(); k++){
                name = name + quotationSublistModelList.get(k).getProName() + ",";
                code = code + quotationSublistModelList.get(k).getProCode() + ",";
            }
            quotationMainModelList.get(i).setProName(name);
            quotationMainModelList.get(i).setProCode(code);
        }
        return quotationMainModelList;
    }

    /**
     * 审核通过
     * @param quotationMainModel
     * @return 审核结果
     */
    public JsonResult appQtt(QuotationMainModel quotationMainModel) {
        JsonResult jsonResult = new JsonResult();
        try {
            VerifyProcessModel verifyProcess = quotationApproveDao.selectNowProcess(quotationMainModel);//根据报价主单id查询当前审核记录
            VerifyProcessModel verifyProcessModel = quotationApproveDao.selectNextProcessNode(quotationMainModel.getQuoteId(),verifyProcess.getNowNode());//查询下一节点verify_info的id
            User user = ShiroUtils.getUser();
            verifyProcess.setVerifier(user.getId());
            verifyProcess.setRemark(quotationMainModel.getAppRemark());
            verifyProcess.setAppStatus(2);
            verifyProcess.setAppTime(new Date());
            if(verifyProcessModel== null || verifyProcessModel.getNextNode() == null)//如果下一节点为空，则不插入下一节点值
            {
                verifyProcess.setStart(null);
            } else {
                verifyProcess.setStart(verifyProcessModel.getNextNode());
            }
            verifyProcessMapper.updateByPrimaryKeySelective(verifyProcess); //更新当前审核流程

            if (verifyProcess.getStart() != null)//如果当前审核流程的下一节点不为空则生成下一审核节点
            {
                VerifyProcess verifyProcessNext = new VerifyProcess();
                verifyProcessNext.setSheetId(verifyProcess.getSheetId());
                verifyProcessNext.setCode(verifyProcess.getCode());
                verifyProcessNext.setComp(verifyProcess.getNowNode());
                verifyProcessNext.setAppStatus(1);
                verifyProcessNext.setNowNode(verifyProcess.getStart());
                verifyProcessMapper.insertSelective(verifyProcessNext);
            }
            if(verifyProcess.getFinishMark() == 0) //如果当前节点的审核结束标识为0，则将报价状态置为3，审核中；如果标识为1，则将报价状态置为4，审核通过
            {
                quotationMainModel.setQuoteStatus(3);
            }else {
                quotationMainModel.setQuoteStatus(4);
                quotationMainModel.setQuoteDate(verifyProcess.getAppTime());
            }
            quotationMainDao.updateQuote(quotationMainModel);//更新报价主单
            quotationMainModel = quotationMainDao.selectQttInfo(quotationMainModel.getQuoteId());
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("审核通过！");
            logService.saveLog("审核通过","报价单号：【"+quotationMainModel.getQuoteNo()+"】【"+user.getName()+"】审核通过!");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("审核失败！");
        }
     return jsonResult;
    }

    /**
     * 审核驳回
     * @param quotationMainModel
     * @return 审核结果
     */
    public JsonResult rejQtt(QuotationMainModel quotationMainModel) {
        JsonResult jsonResult = new JsonResult();
        try {
        QuotationMain quotationMain = quotationMainMapper.selectByPrimaryKey(quotationMainModel.getQuoteId());
        VerifyProcessModel verifyProcess = quotationApproveDao.selectNowProcess(quotationMainModel);
        VerifyProcessModel verifyProcessModel = quotationApproveDao.selectPreProcessNode(quotationMainModel.getQuoteId(),verifyProcess.getNowNode());
        User userCreate = quotationApproveDao.selectUserInfo(quotationMain.getCreateAccount());
        VerifyProcessModel verifyProcessModel1 = quotationMainDao.selectStartNode(userCreate.getId());//根据创建账号查找起始节点
        User user = ShiroUtils.getUser();
        verifyProcess.setVerifier(user.getId());
        verifyProcess.setRemark(quotationMainModel.getAppRemark());
        verifyProcess.setAppStatus(0);
        verifyProcess.setAppTime(new Date());
        if(verifyProcessModel1 == null)//根据起始节点和审核驳回的下一节点判断当条审核是否有下一节点
        {
            verifyProcess.setStart(null);
        }else{
            if(verifyProcessModel != null){
                if(verifyProcessModel.getPreNode() < verifyProcessModel1.getNowNode()){
                    verifyProcess.setStart(null);
                }else{
                    verifyProcess.setStart(verifyProcessModel.getPreNode());
                }
            }else {
                verifyProcess.setStart(null);
            }
        }
        verifyProcessMapper.updateByPrimaryKeySelective(verifyProcess); //更新当前审核流程
        if (verifyProcess.getStart() != null)//如果当前审核流程的下一节点不为空则生成下一审核节点
        {
            VerifyProcess verifyProcessNext = new VerifyProcess();
            verifyProcessNext.setSheetId(verifyProcess.getSheetId());
            verifyProcessNext.setCode(verifyProcess.getCode());
            verifyProcessNext.setComp(verifyProcess.getNowNode());
            verifyProcessNext.setAppStatus(1);
            verifyProcessNext.setNowNode(verifyProcess.getStart());
            verifyProcessMapper.insertSelective(verifyProcessNext);
        }
        if(verifyProcess.getStart() == null) //如果当前审核无下一节点，则将报价状态置为5，审核驳回；否则，则将报价状态置为3，审核中
        {
            quotationMainModel.setQuoteStatus(5);
        }else {
            quotationMainModel.setQuoteStatus(3);
        }
            quotationMainDao.updateQuote(quotationMainModel);//更新报价主单
            quotationMainModel = quotationMainDao.selectQttInfo(quotationMainModel.getQuoteId());
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("审核已驳回！");
            logService.saveLog("审核驳回","报价单号：【"+quotationMainModel.getQuoteNo()+"】【"+user.getName()+"】审核驳回!");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("驳回失败！");
        }
        return jsonResult;
    }


//    public List<QuotationMainModel> findAppCommentList(QuotationMainModel query) {
//        return quotationApproveDao.selectAppCommentList(query);
//    }
}
